import { Models } from '../../../../common/typings/model'
import KoaRouter from 'koa-router'
import Mysql from '../../../../core/Mysql'
import { Success } from '../../../../core/HttpException'
import validator from '../../../../middlewares/validator'
import Auth from '../../../../middlewares/Auth'
import editUserById from '../../../../common/apiValidate/system/user/editUserById'
import Config from '../../../../config/Config'
const router = new KoaRouter({
  prefix: `${Config.api_prefix}system/user`,
})
/**
 *
 * @api {post} /system/user/editUserById 编辑用户信息
 * @apiGroup system
 * @apiVersion 0.1.0
 *
 * @apiParam  {String} nickName 昵称
 * @apiParam  {String} profile 描述
 * @apiParam  {String} avatar 头像
 *
 * @apiSuccess {number} errorCode 错误码
 * @apiSuccess {string} data 返回值
 * @apiSuccess {string} msg ok
 *
 * @apiParamExample  {type} Request-Example:
 * {
 *    nickName: ''
 *    profile: ''
 *    avatar: ''
 * }
 *
 *
 * @apiSuccessExample {type} Success-Response:
 * {
 *    errorCode: 0
 *    data: "",
 *    msg: "ok"
 * }
 *
 *
 */
router.post('/editUserById', Auth.verifyTokenPermission, validator(editUserById, 'body'), async (ctx: Models.Ctx) => {
  const { nickName, profile = '', avatar, roleId, id } = ctx.request.body
  const info = {
    nickName,
    profile,
    avatar,
  }
  await Mysql.command(`
    UPDATE
      user
    SET info = '${JSON.stringify(info)}', role_id = ${roleId}
    WHERE id = ${id}
  `)

  throw new Success()
})

export default router
